﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// TagListView.xaml 的交互逻辑
    /// </summary>
    public partial class TagListView : UserControl
    {
        Dictionary<Label, string> _tags;

        public List<string> Tags
        {
            set
            {
                UpdateTags(value);
            }
            get
            {
                return _tags.Values.ToList<string>();
            }
        }

        public TagListView()
        {
            InitializeComponent();
        }

        public void UpdateTags(List<string> tags)
        {
            this.dockPanel1.Children.Clear();
            _tags = new Dictionary<Label, string>();

            foreach (string tag in tags)
            {
                Label label = new Label();
                label.Content = tag;
                label.FontSize = 16;
                label.BorderThickness = new Thickness(1);
                label.BorderBrush = Brushes.Gray;
                label.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                label.MouseDoubleClick += new MouseButtonEventHandler(label_MouseDoubleClick);
                _tags.Add(label, tag);
                dockPanel1.Children.Add(label);
            }

            TextBox newtagbox = new TextBox();
            newtagbox.Text = "(New Tag)";
            newtagbox.FontSize = 16;
            newtagbox.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            newtagbox.GotFocus += new RoutedEventHandler(delegate(object o, RoutedEventArgs e){
                newtagbox.Text = "";
            });
            newtagbox.KeyUp += new KeyEventHandler(delegate(object o, KeyEventArgs e){
                if (e.Key == Key.Enter)
                {
                    newtag_LostFocus(o, e);
                }
            });
            newtagbox.LostFocus += new RoutedEventHandler(newtag_LostFocus);
            dockPanel1.Children.Add(newtagbox);
        }

        
        #region EventHandleMethods

        public static readonly RoutedEvent TagEvent = EventManager.RegisterRoutedEvent("Tag", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(TagListView));

        public event RoutedEventHandler TagEventHandler
        {
            add
            {
                AddHandler(TagEvent, value);
            }
            remove
            {
                RemoveHandler(TagEvent, value);
            }
        }

        void label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            string tag = _tags[(Label)sender];
            TagEventArgs tagEventArgs = new TagEventArgs(false, tag, TagEvent);
            RaiseEvent(tagEventArgs);
        }

        void newtag_LostFocus(object sender, RoutedEventArgs e)
        {
            string tag = ((TextBox)sender).Text;
            if (tag.Equals(""))
            {
                ((TextBox)sender).Text = "(New Tag)";
                return;
            }
            else
            {
                TagEventArgs tagEventArgs = new TagEventArgs(true, tag, TagEvent);
                RaiseEvent(tagEventArgs);
            }
            
        }

        #endregion
    }

    public class TagEventArgs : RoutedEventArgs
    {
        bool _isNew;
        string _tag;

        public TagEventArgs(bool isNew, string tag, RoutedEvent e)
            : base(e)
        {
            _isNew = isNew;
            _tag = tag;
        }

        public bool IsNew
        {
            get
            {
                return _isNew;
            }
        }

        public string Tag
        {
            get
            {
                return _tag;
            }
        }
    }
}
